package com.bw.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.bw.entity.*;
import com.bw.mapper.JobInfoMapper;
import com.bw.service.JobInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelExtensionsKt;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class JobController {

    @Autowired
    JobInfoMapper jobInfoMapper;

    @Autowired
    JobInfoService jobInfoService;

    @RequestMapping("/list.do")
    public String list(JobQuery query, @RequestParam(defaultValue = "1") Integer pageNum, Model model, HttpServletRequest request) {
        //获取当前登录用户
        User userFromRequest = TokenUtils.getLoginUserFromRequest(request, User.class);
        System.out.println("获取到的当前登录用户 " + JSON.toJSONString(userFromRequest));
        // 1. 下拉框数据渲染
        selectData(model);

        //2. 分页查询
        PageHelper.startPage(pageNum, 10); // 和 需要分页的SQL紧挨着写
        List<JobInfo> list = jobInfoMapper.selectByParam(query);
        PageInfo<JobInfo> pageInfo = new PageInfo<>(list);

        model.addAttribute("pageInfo", pageInfo);

        //3.条件回显
        model.addAttribute("query", query);
        return "list";
    }


    @RequestMapping("/del.do")
    public String del(String id) {
        jobInfoMapper.del(id);

        return "redirect:list.do";

    }

    @RequestMapping("/addPage.do")
    public String addPage(Model model) {
        // 1. 下拉框数据渲染
        selectData(model);

        return "add";

    }


    @RequestMapping("/add.do")
    public String add(@Valid JobInfoAdd jobInfoAdd, BindingResult result, Model model) {
        //1. 非空验证  JSR303验证
        if (result.hasErrors()) {

            model.addAttribute("error", result.getAllErrors().get(0).getDefaultMessage());
            // 渲染下拉框数据
            List<Dict> postList = jobInfoMapper.listPost();
            model.addAttribute("postList", postList);
            List<Dict> schoolList = jobInfoMapper.listSchool();
            model.addAttribute("schoolList", schoolList);
            return "add";  // 全新的添加页面
        }
        //员工编号以“GH”开始
        String num = jobInfoAdd.getNum();
        if (!num.startsWith("GH")) {
            model.addAttribute("error", "员工编号以“GH”开始");
            // 渲染下拉框数据
            List<Dict> postList = jobInfoMapper.listPost();
            model.addAttribute("postList", postList);
            List<Dict> schoolList = jobInfoMapper.listSchool();
            model.addAttribute("schoolList", schoolList);
            return "add";  // 全新的添加页面
        }
        // 编号不能重复
        JobInfo jobInfoDB = jobInfoMapper.selectByNum(jobInfoAdd.getNum());
        if (jobInfoDB != null){
            model.addAttribute("error", "编号不能重复");
            // 渲染下拉框数据
            List<Dict> postList = jobInfoMapper.listPost();
            model.addAttribute("postList", postList);
            List<Dict> schoolList = jobInfoMapper.listSchool();
            model.addAttribute("schoolList", schoolList);
            return "add";  // 全新的添加页面

        }
        //开始时间小于结束时间
        String startTime = jobInfoAdd.getStartTime();
        DateTime start = DateUtil.parse(startTime, "yyyy-MM-dd");

        String endTime = jobInfoAdd.getEndTime();
        DateTime end = DateUtil.parse(endTime, "yyyy-MM-dd");

        if (start.isAfter(end)){
            model.addAttribute("error", "开始时间小于结束时间");
            // 渲染下拉框数据
            List<Dict> postList = jobInfoMapper.listPost();
            model.addAttribute("postList", postList);
            List<Dict> schoolList = jobInfoMapper.listSchool();
            model.addAttribute("schoolList", schoolList);
            return "add";  // 全新的添加页面
        }

        // 执行添加
        jobInfoService.addJob(jobInfoAdd);

        return "redirect:list.do";

    }

    /**
     * 给两个下拉框数据赋值
     *
     * @param model
     */
    private void selectData(Model model) {

        List<Dict> postList = jobInfoMapper.listPost();
        model.addAttribute("postList", postList);

        List<Dict> schoolList = jobInfoMapper.listSchool();
        model.addAttribute("schoolList", schoolList);
    }
}
